package com.bw.a1908.ui.video;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bw.a1908.R;
import com.bw.a1908.adapter.VideoFullScreenAdapter;
import com.bw.a1908.adapter.VideoItemAdapter;
import com.bw.a1908.adapter.VideoItemDecoration;
import com.bw.a1908.base.LazyFragment;
import com.bw.a1908.bean.VideoBean;
import com.bw.a1908.contract.VideoContract;
import com.bw.a1908.core.mvp.BaseFragment;
import com.bw.a1908.presenter.VideoPresenter;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.List;

import static com.shuyu.gsyvideoplayer.video.base.GSYVideoView.CURRENT_STATE_PAUSE;
import static com.shuyu.gsyvideoplayer.video.base.GSYVideoView.CURRENT_STATE_PLAYING;

/**
 * 刷起来
 */
public class VideoFullScreenFragment extends LazyFragment<VideoPresenter> implements VideoContract.View {

    private RecyclerView recyclerView;
    private VideoFullScreenAdapter mAdapter;
    private PagerSnapHelper snapHelper;
    private LinearLayoutManager linearLayoutManager;

    private int currPosition = 0;

    @Override
    public void initAdapter(List<VideoBean> videoBeans) {
        if (mAdapter == null) {
            mAdapter = new VideoFullScreenAdapter(videoBeans);
            recyclerView.setAdapter(mAdapter);
        }
    }

    @Override
    public int bandLayout() {
        return R.layout.fragment_video_card_list;
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.fragment_video_rv);
        recyclerView.setLayoutManager(linearLayoutManager = new LinearLayoutManager(getContext()));
        //处理RecyclerView在手势离开屏幕后，惯性滑动结束，整页Item停留位置
        snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(recyclerView);
        //子Item绑定状态改变时回调
        recyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(@NonNull View view) {
            }

            @Override
            public void onChildViewDetachedFromWindow(@NonNull View view) {

            }
        });
        //监听滑动状态
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //切入点 - 状态为 0 时
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    //空闲状态
                    View snapView = snapHelper.findSnapView(linearLayoutManager);
                    int position = linearLayoutManager.getPosition(snapView);
                    currPosition = position;

                    StandardGSYVideoPlayer player = (StandardGSYVideoPlayer) mAdapter.getViewByPosition(position, R.id.item_video_player);
                    //优化
                    if (player.getCurrentState() != CURRENT_STATE_PLAYING) {
                        player.startPlayLogic();
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }

    @Override
    public void initData() {
        mPresenter = new VideoPresenter(this);
    }

    @Override
    protected void lazy() {
        mPresenter.list();
    }

    @Override
    public void onUserStatue(boolean isShow) {
        LogUtils.d("isShow-" + isShow);
        if (mAdapter == null) {
            return;
        }
        StandardGSYVideoPlayer player = (StandardGSYVideoPlayer) mAdapter.getViewByPosition(currPosition, R.id.item_video_player);
        int statue = player.getCurrentState();

        switch (statue){
            case CURRENT_STATE_PLAYING:
                //播放中
                if (!isShow){
                    player.onVideoPause();
                }
                return;
            case CURRENT_STATE_PAUSE:
                //播放中
                if (isShow){
                    player.onVideoResume();
                }
                return;
        }
    }
}
